package com.df.happylearn.model;

import android.util.Log;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

@DatabaseTable(tableName = "table_message")
public class MessageInfo {
    @DatabaseField(id = true, uniqueCombo = true, columnName = "messageId")
    public int mId;
    @DatabaseField(columnName = "type")
    public String mType;
    @DatabaseField(columnName = "isRead")
    public int mIsRead;
    @DatabaseField(columnName = "sendId")
    public int mSendId;
    @DatabaseField(columnName = "contend")
    public String mContent;
    @DatabaseField(columnName = "linkNewsId")
    public String mLinkNewsId;
    @DatabaseField(columnName = "ctime")
    public String mCtime;
    @DatabaseField(columnName = "newTitle")
    public String mNewsTitle;
    @DatabaseField(columnName = "beread")
    public int beread; //消息是否已读

    //json对象解析
    public static MessageInfo parseJson(JSONObject jsonObject) {
        try {
            MessageInfo item = new MessageInfo();
            if (jsonObject.has("id")) {
                item.mId = Integer.parseInt(jsonObject.getString("id"));
            }
            if (jsonObject.has("type")) {
                item.mType = jsonObject.getString("type");
            }
            if (jsonObject.has("is_read")) {
                item.mIsRead = Integer.parseInt(jsonObject.getString("is_read"));
            }
            if (jsonObject.has("send_id")) {
                item.mSendId = Integer.parseInt(jsonObject.getString("send_id"));
            }
            if (jsonObject.has("content")) {
                item.mContent = jsonObject.getString("content");
            }
            if (jsonObject.has("link_news_id")) {
                item.mLinkNewsId = jsonObject.getString("link_news_id");
            }
            if (jsonObject.has("ctime")) {
                item.mCtime = jsonObject.getString("ctime");
            }
            if (jsonObject.has("news_title")) {
                item.mNewsTitle = jsonObject.getString("news_title");
            }
            return item;
        }catch (Exception e) {
            return null;
        }
    }

    //jsonArray对象解析
    public static ArrayList<MessageInfo> parseJsonArray(JSONArray jsonArray) {
        try {
            ArrayList<MessageInfo> listItem = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                MessageInfo item = MessageInfo.parseJson(jsonArray.getJSONObject(i));
                if (item != null) {
                    listItem.add(item);
                }
            }
            return listItem;
        }catch (Exception e) {
            return null;
        }
    }
}
